/**
 * 
 */
package com.gr.staffpm.tasks.model;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.joda.time.DateTime;
import org.joda.time.Instant;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.pages.tasks.ViewTask;


/**
 * @author Graham Rhodes 5 Apr 2011 16:02:59
 */
public class TaskPanel extends Panel {

    private static final long serialVersionUID = 1L;
    private final Label taskContent;
    private final BookmarkablePageLink<Void> contentLink;
    private final Label timestampLabel;

    public TaskPanel(String id, final Task task) {
        super(id);

        contentLink = new BookmarkablePageLink<Void>("contentLink", ViewTask.class, new PageParameters("id=" + task.getTaskId()));
        contentLink.setOutputMarkupId(true);

        taskContent = new Label("taskContent", task.getName() + " - " + task.getSummary());
        taskContent.setOutputMarkupId(true);
        contentLink.add(taskContent);

        final DateTime taskDueDate = new DateTime(task.getDueDate());
        Period period = new Period(new Instant(), taskDueDate, PeriodType.standard());
        PeriodFormatter periodFormat =
                new PeriodFormatterBuilder().appendWeeks().appendSuffix(" week", " weeks").appendSeparator(", ").appendDays().appendSuffix(" day", " days").appendSeparator(", ").appendHours().appendSuffix(
                        " hour", " hours").appendSeparator(", ").appendMinutes().appendSuffix(" minute", " minutes").toFormatter();

        timestampLabel = new Label("timestamp", periodFormat.print(period)) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(ComponentTag tag) {
                tag.setName("abbr");
                tag.getAttributes().put("title", DateTimeFormat.forPattern("EEEEE, d MMMMM yyyy 'at' HH:mm").print(taskDueDate));
            };
        };
        timestampLabel.setOutputMarkupId(true);

        contentLink.add(timestampLabel);
        add(contentLink);
    }

}
